//打印模板预览
OB.Lodop_Print = {};

//预览
OB.Lodop_Print.Lodop_Print_Preview = function(params, view) {
	var viewGrid = view.activeView.viewGrid;
	var record = viewGrid.getSelectedRecord();
	var LODOP = getCLodopA();
	LODOP.PRINT_INIT("");
	LODOP.SET_SHOW_MODE("HIDE_SBUTTIN_PREVIEW", 1);
	LODOP.SET_SHOW_MODE("HIDE_QBUTTIN_PREVIEW", 1);
	LODOP.SET_SHOW_MODE("PREVIEW_NO_MINIMIZE", 1);
	LODOP.SET_PRINT_MODE("AUTO_CLOSE_PREWINDOW", 1);
	LODOP.SET_PREVIEW_WINDOW(2, 0, 1, 760, 540, ".打印");
	
	var list = [{}];
	
	var printScript = record.html;
	if (printScript) {
		var print_method = "";
		
		if(printScript.indexOf("LODOP.PREVIEW") > -1) {
			print_method = "LODOP.PREVIEW();";
		} else if(printScript.indexOf("LODOP.PRINT_DESIGN") > -1) {
			print_method = "LODOP.PRINT_DESIGN();";
		} else if(printScript.indexOf("LODOP.PRINT_SETUP") > -1) {
			print_method = "LODOP.PRINT_SETUP();";
		} else if(printScript.indexOf("LODOP.PRINT()") > -1) {
			print_method = "LODOP.PRINT();";
		}
		var printScript = printScript;
		if(print_method){
			printScript = printScript.replace(print_method, "");
		}
		printScript = printScript.replace("LODOP.PRINT_INIT(\"\");", "");
		eval(printScript);
	}
	LODOP.PREVIEW();
}

//设计打印模板
OB.Lodop_Print.Lodop_Print_Design = function(params, view) {
	var viewGrid = view.activeView.viewGrid;
	var record = viewGrid.getSelectedRecord();
	var LODOP = getCLodopA();
	var printScript = record.html;
	var print_method = "";
	barCodeDesign = true;
	
	var list = [{}];
	
	if (printScript) {
		if(printScript.indexOf("LODOP.PREVIEW") > -1) {
			print_method = "LODOP.PREVIEW();";
		} else if(printScript.indexOf("LODOP.PRINT_DESIGN") > -1) {
			print_method = "LODOP.PRINT_DESIGN();";
		} else if(printScript.indexOf("LODOP.PRINT_SETUP") > -1) {
			print_method = "LODOP.PRINT_SETUP();";
		} else if(printScript.indexOf("LODOP.PRINT()") > -1) {
			print_method = "LODOP.PRINT();";
		}
		if(print_method){
			printScript = printScript.replace(print_method, "");
		}
		eval(printScript);
	}
	
	//关闭窗口回调函数
	LODOP.On_Return = function(taskID, value) {
		var htmlCode = value;
		
		if(htmlCode != record.html){
			isc.confirm("是否修改模板？", function(bool){
				if (bool) {
					htmlCode = htmlCode.replace("LODOP.PRINT_INIT(\"\");", "");
					OB.RemoteCallManager.call("com.basis.barcode.BarCodeLabelUpdateHandler",
						{id : record.id, html: htmlCode}, {},
						function(response, data, request) {
							if(data.result) {
                                view.refresh();
							} else {
								isc.showMessage(data.msg);
							}
						}
					);
				}
			});
		}
	};
	LODOP.PRINT_DESIGN();
}

//导出打印模板
OB.Lodop_Print.Lodop_Print_Export = function(params, view) {
	var viewGrid = view.activeView.viewGrid;
	var record = viewGrid.getSelectedRecord();
	var scriptCode = record.html;
	var LODOP = getCLodopA();
	if (LODOP.CVERSION) {
		LODOP.On_Return = function(TaskID,Value){
			var strFilename = Value;
			LODOP.On_Return = function(TaskID,Value){
				isc.showMessage(Value);
			};
			LODOP.WRITE_FILE_TEXT(0,strFilename, scriptCode);
		}
	}
	var strFilename=LODOP.GET_DIALOG_VALUE("LocalFileFullNameforSave", "打印模板.MES");
 	if (!LODOP.CVERSION) {
 		isc.showMessage(LODOP.WRITE_FILE_TEXT(0,strFilename,scriptCode));
	}
}

//导入打印模板
OB.Lodop_Print.Lodop_Print_Import = function(params, view) {
	var viewGrid = view.activeView.viewGrid;
	var record = viewGrid.getSelectedRecord();
	var LODOP = getCLodopA();
	if (LODOP.CVERSION) {
		LODOP.On_Return = function(TaskID, Value){
			var strFilename = Value;
			LODOP.On_Return = function(TaskID, value){
				OB.RemoteCallManager.call("com.basis.barcode.BarCodeLabelUpdateHandler", 
					{id : record.id, html: value}, {}, 
					function(response, data, request){
						if(data.result){
							isc.showMessage("导入成功！");
							viewGrid.refreshGrid();
						}else{
							isc.showMessage(data.msg);
						}
					}
				);
			};
			LODOP.GET_FILE_TEXT(strFilename);
		}
	}
	var strFilename = LODOP.GET_DIALOG_VALUE("LocalFileFullNameforOpen", "打印模板.MES");
 	if (!LODOP.CVERSION) {
		return LODOP.GET_FILE_TEXT(strFilename);
	};	
}